/*
 * @Description: 加解密
 * @Author: chenju
 * @Date: 2020-06-11 10:33:14
 * @LastEditors: chenju
 * @LastEditTime: 2021-06-08 15:05:56
 */
import CryptoJS from 'crypto-js';
export const encrypt = (word: string, keyStr?: string): string => {
  keyStr = keyStr || 'geelyzhongjianjk';
  const key = CryptoJS.enc.Utf8.parse(keyStr); // Latin1 w8m31+Yy/Nw6thPsMpO5fg==
  const srcs = CryptoJS.enc.Utf8.parse(word);
  const encrypted = CryptoJS.AES.encrypt(srcs, key, {
    iv: key,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.ZeroPadding
  });
  return encrypted.toString();
};

export const decrypt = (word: string, keyStr: string): string => {
  console.log(word, keyStr);
  keyStr = keyStr || 'geelyzhongjianjk';
  const key = CryptoJS.enc.Utf8.parse(keyStr); // Latin1 w8m31+Yy/Nw6thPsMpO5fg==
  const decrypt = CryptoJS.AES.decrypt(word, key, {
    iv: key,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.ZeroPadding
  });
  return CryptoJS.enc.Utf8.stringify(decrypt).toString();
};

// 加签 HmacSHA256
export const addSignature = (message: string, key: string): string => {
  return CryptoJS.HmacSHA256(message, key).toString();
};
